<?php
  class template
{
      /**
    *    @desc        whatever
    *    @access    public
    *    @var        boolean
    */
    private $use_request_handler = true;

    /**
    *    @desc        array to store blocks for the page
    *                 we use this only when we have to update a whole page
    *    @access    public
    *    @var        array
    */
    public $page_blocks = array();

    /**
    *    @desc        array to store HTML elements to be updated
    *    @access    private
    *    @var        array
    */
    private $updated_elements = array();

    /**
    *    @desc        ID of the page where we request from
    *    @access        private
    *    @var        string
    */
    private $caller;

    /**
    *    @desc        store pointer to builtin smarty template manager
    *    @access    private
    *    @var        Smarty
    */
    private $smarty;

    /**
    *    @desc        the template has been setup yet
    *    @access    private
    *    @var        boolean
    */
    private $setup_done = false;

    /**
    *    @desc        cache time of the cache types in system
    *    @access    private
    *    @var        array
    */
    private $cache_time = array();

    /**
    *    @desc        pointer the default layout structure
    *    @access    private
    *    @var        object
    */
    private $default_struct;
    
    private $config;

    
    public function __construct()
    {     
        require_once(PATH_INCLUDES. "/Smarty/Smarty.class.php");
        require_once(PATH_CODE. "/config.php");   
        $this->config = new config();
        // Create smarty
        $this->smarty = new Smarty();
        /**
        * init smarty
        */
        $this->_init_smarty();  
         /**
        * check handler we're using
        */
        $this->_init_handler(); 
        //$this->smarty->register_function("fckeditor", "fckeditor");  
    }
    
    public function autorun()
    {
    }   
    
    /**
    *    @desc        init the handler
    *    @access        protected
    *    @param        void
    *    @returns    void
    */
    protected function _init_handler()
    {
        $this->caller  = isset($_GET['sendby'])?$_GET['sendby'] : 'browser';
    }
     /**
    *    @desc        init for smarty
    *    @access        private
    *    @param        void
    *    @returns    void
    *   @internal
    */
    private function _init_smarty()
    {
        /**
        * directory use in smarty engine
        */
        $this->smarty->template_dir =   PATH_ROOT. '/' . $this->config->template_dir;
        $this->smarty->compile_dir  =   PATH_ROOT. '/' .$this->config->compile_dir;
        $this->smarty->cache_dir    =   PATH_ROOT. '/' .$this->config->cache_dir;

        /**
        * default cache
        */
        //$this->smarty->use_sub_dirs = TRUE;
    }
    /**
    *    @desc        send var to template
    *    @access        private
    *    @param        void
    *    @returns    void
    *   @internal
    */
    public function send_var($var_name, $var_value)
    {
        $this->smarty->assign($var_name, $var_value);
    }
    
    /**
    *    @desc        send a pointer to template file
    *
    *    @access        public
    *    @param        string
    *     @param        object
    *    @returns    void
    *     @throws        lbp_base_error        #1013
    */
    public function send_pointer($pointer_name, $pointer_value)
    {
        $this->smarty->assign_by_ref($pointer_name, $pointer_value);
    }
     /**
    *    @desc        get a template file content
    *    @access        public
    *    @param        string
    *    @returns    string
    */
    public function get_template_file($template_file_name = '')
    {
      $this->smarty->clear_all_cache(); 
      return $this->smarty->display($template_file_name);
    }
    
    public function load_template_file($template_file_name = ''){
        $this->smarty->clear_all_cache();
       return $this->smarty->fetch($template_file_name);    
    }
    /**
    *    @desc        update an element by the html
    *    @access        public
    *    @param        string
    *     @param        string
    *    @returns    void
    */
    public function update_element($element_id, $element_html)
    {
        print $element_html;
        $this->updated_elements[ $element_id ] = $element_html;
    }
    /**
    *    @desc        run it now
    *    @access        public
    *    @param        void
    *    @returns    void
    */
    public function go()
    {
        //print_r($this->updated_elements);
        $json_output = json_encode($this->updated_elements);

        //print $json_output;
    }
    //Create editor
    function fckeditor($name='des',$value='',$width='600',$height='350'){
        require_once(PATH_INCLUDES."/fckeditor/fckeditor.php");
        $oFCKeditor = new FCKeditor($name) ;
        $oFCKeditor->BasePath    = PATH_INCLUDES."/fckeditor/editor/" ;
        $oFCKeditor->Value        = $value;
        $oFCKeditor->Height        = $height;
        $oFCKeditor->width        =  $width;
        $oFCKeditor->ToolbarSet    = 'basic';
        return $oFCKeditor->Create() ;
    }  
}
?>
